package com.study.service.db3.impl;

import java.util.HashSet;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import com.study.entity.db3.Resource;
import com.study.entity.db3.vo.ResourceVO;
import com.study.mapper.db3.ResourceMapper;
import com.study.service.db3.IResourceService;
import com.study.util.treeutil.TreeNodeConfig;
import com.study.util.treeutil.TreeNodeMap;
import com.study.util.treeutil.TreeUtil_4_0;
import com.study.util.treeutil.TreeUtil_4_0.Convert;

/**
 * 资源表 服务实现类
 * 
 * @author chenk
 *
 */
@Service
public class ResourceServiceImpl implements IResourceService {

	@Autowired
	private ResourceMapper resourceMapper;

	@Override
	public List<TreeNodeMap> listResource(List<Long> roleIds, Integer flag) {
		if (roleIds == null || roleIds.size() <= 0) {
			List<Resource> listResource = resourceMapper.listResource();
			TreeNodeConfig treeNodeConfig = new TreeNodeConfig();
			// 自定义属性名
			treeNodeConfig.setNameKey("title");
			List<TreeNodeMap> treeNodeMaps = TreeUtil_4_0.build(listResource, "0", treeNodeConfig,
					new Convert<Resource, TreeNodeMap>() {
						@Override
						public void convert(Resource resource, TreeNodeMap treeNode) {
							treeNode.setId(String.valueOf(resource.getResourceId()));
							treeNode.setParentId(String.valueOf(resource.getParentId()));
							treeNode.setName(resource.getResourceName());
						}

					});
			return treeNodeMaps;
		} else {
			// flag =1 详情 flag =0 修改
			List<ResourceVO> listResource = resourceMapper.listResourceByRoleId(roleIds, flag);
			TreeNodeConfig treeNodeConfig = new TreeNodeConfig();
			// 自定义属性名
			treeNodeConfig.setNameKey("title");

			List<TreeNodeMap> treeNodeMaps = TreeUtil_4_0.build(listResource, "0", treeNodeConfig,
					new Convert<ResourceVO, TreeNodeMap>() {

						@Override
						public void convert(ResourceVO resource, TreeNodeMap treeNode) {
							treeNode.setId(String.valueOf(resource.getResourceId()));
							treeNode.setParentId(String.valueOf(resource.getParentId()));
							treeNode.setName(resource.getResourceName());
							treeNode.put("url", resource.getUrl());
							// 如果不是叶子节点，一律设置成false（此处就两级菜单，暂不处理）
							treeNode.put("checked", resource.getParentId() == 0 ? false : resource.isChecked());
						}
					});

			return treeNodeMaps;
		}
	}

	@Override
	public HashSet<String> convert(List<TreeNodeMap> resourceVOs) {
		// TODO 递归处理
		HashSet<String> module = new HashSet<>();
		resourceVOs.forEach(r -> {
			String url = (String) r.get("url");
			if (StringUtils.hasLength(url)) {
				module.add(url.substring(0, url.indexOf("/")));
			}

			List<TreeNodeMap> subResources = r.getChildren();
			if (subResources != null && subResources.size() > 0) {
				subResources.forEach(sub -> {
					String subUrl = (String) sub.get("url");
					if (StringUtils.hasLength(subUrl)) {
						module.add(subUrl.substring(0, subUrl.indexOf("/")));
					}
				});
			}
		});
		return module;
	}

}
